package cc.android.testapp.ui.applist;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.File;

import cc.android.testapp.R;
import cc.android.testapp.TAContentProvider;
import cc.android.testapp.ui.listbase.BaseLine;
import cc.android.testapp.ui.listbase.BaseListView;
import cc.android.testapp.ui.listbase.LineData;
import cc.android.testapp.xp.hooker.adblock.util.ADBCfg;

public class AppLiseView extends BaseListView<AppLiseView.AppInfo, AppLiseView.AppLine> {

    public AppLiseView(AppInfo[] pApps, int pLineLayout) {
        super(pApps, pLineLayout);
    }

    @Override
    public AppLine getNewLine(View pLineV) {
        return new AppLine(pLineV);
    }

    public static class AppInfo extends LineData {
        final public String name;
        final public Drawable icon;
        final public ApplicationInfo mApp;

        public AppInfo(PackageManager pPM, ApplicationInfo pApp) {
            this.name = pApp.loadLabel(pPM).toString();
            this.icon = pApp.loadIcon(pPM);
            this.mApp = pApp;
        }

        @Override
        public String getId() {
            return this.name;
        }

        @Override
        public int getWeight() {
            File tFile = ADBCfg.getAppRuleConfig(this.mApp.packageName);
            int tWeight = 0;
            if (tFile.list() != null && tFile.list().length > 0) tWeight--;
            if ((this.mApp.flags & ApplicationInfo.FLAG_SYSTEM) == 0) tWeight--;
            return tWeight;
        }
    }

    public static class AppLine extends BaseLine<AppInfo> {
        public AppLine(View pContent) {
            super(pContent);
        }

        @Override
        public void setData(AppInfo pApp) {
            super.setData(pApp);

            TextView tTV = (TextView) this.itemView.findViewById(R.id.tv_app_name);
            String tName = pApp.name;
            File tFile = ADBCfg.getAppRuleConfig(pApp.mApp.packageName);
            if (tFile.list() != null && tFile.list().length > 0) {
                tName = "\uD83D\uDE00" + tName;
            }
            tTV.setText(tName);
            tTV = (TextView) this.itemView.findViewById(R.id.tv_app_package);
            tTV.setText(pApp.mApp.packageName);

            ImageView tIV = (ImageView) this.itemView.findViewById(R.id.iv_app_icon);
            tIV.setImageDrawable(this.mData.icon);
        }

    }

}
